package ru.startandroid.firstproj.firstproj;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

public class functions {
    private static int size = 17;
    public static int rand_g(int d)
    {
        if ( d < 1 )
            d = 1;
        return (int)((Math.random()*1000000) % d);
    }
    public static int least(int a, int b, int c, int d )
    {
    	int min;
    	if ( a != -1 )
    		min = a;
    	else
    		if ( b != -1 )
    			min = b;
    		else
    			if ( c != -1 )
    				min = c;
    			else
    				min = d;

    	if ( min > b && b != -1 )
    		min = b;
    		if ( min > c && c != -1 )
    			min = c;
    			if ( min > d && d != -1 )
    				min = d;
    				return min;
    }
    public static int sqr(int w)
    {
        return w*w;
    }
    public static boolean equal(int[] q, int[] p, int size)
    {
        for (int i = 0; i < size; ++i)
            if (q[i] != p[i])
                return false;
        return true;
    }
    public static boolean find(maze m,
                        list <Integer, Integer> path,
                        list <Integer, Integer> fire,
    		            int yi, int xi, int y2, int x2)
    {
    	boolean r=false, l=false, up=false, d=false;					
    	int up_diag=-1, d_diag=-1, l_diag=-1, r_diag=-1;

            if (m.elem(yi - 1, xi) != 0 && fire.search(yi - 1, xi) == null) {
                up_diag = sqr(yi - 1 - y2) + sqr(xi - x2);
                up = true;
            }

            if (m.elem(yi + 1, xi) != 0 && fire.search(yi + 1, xi) == null) {
                d_diag = sqr(yi + 1 - y2) + sqr(xi - x2);
                d = true;
            }

            if (m.elem(yi, xi - 1) != 0 && fire.search(yi, xi - 1) == null) {
                l_diag = sqr(yi - y2) + sqr(xi - 1 - x2);
                l = true;
            }

            if (m.elem(yi, xi + 1) != 0 && fire.search(yi, xi + 1) == null) {
                r_diag = sqr(yi - y2) + sqr(xi + 1 - x2);
                r = true;
            }

    	if ( up || d || l || r ) 
    	{
    		int min = least(up_diag, d_diag, l_diag, r_diag);

    		if ( min == d_diag ) ++yi;
    		else
    			if ( min == up_diag ) --yi;
    			else
    				if ( min == l_diag ) --xi;
    				else ++xi;

    		path.push_back(yi,xi);
    		fire.push_back(yi,xi);
    		return true;
    	}
    	else 
    	{
    		if ( fire.search(yi,xi) == null ) 
    			fire.push_back(yi,xi);

    		if ( path.end() != null ) 
    		{	
    			path.pop( path.end() );
    			return ( path.end() != null );
    		}
    		else
    			return false;
    	}
    }
    public static void frame_maze(Canvas canvas, maze m)
    {
        int CanvasWidth = canvas.getWidth(), CanvasHeight = canvas.getHeight();
        int width = CanvasWidth / size, height = CanvasHeight / size;
        if ( width % 2 == 0 )
            width--;
        if ( height % 2 == 0 )
            height--;

        m.set(height, width);
        m.generate();
    }
    public static void draw_maze(Canvas canvas, Paint p, Rect rect, maze m, object player, Rect rectClip, byte[] lurd)
    {
        int width = m.get().x;
        int height = m.get().y;
        int size = canvas.getWidth() / (width + 1);
        if ( ((size + 1) * width) > (canvas.getWidth() / width * width) )
            size--;

        int shift = (canvas.getWidth() - (size + 1) * width) / 2;

        for (int i = 0; i < height; i++)
            for (int j = 0; j < width; j++)
            {
                if ( m.elem(i, j) == 0 )
                    draw_stone(canvas, p, rect, i * size + i, j * size + shift + j);

                if (m.elem(i, j) < 17)
                    draw_lawn(canvas, p, rect, i * size + i, j * size + shift + j, m, player, rectClip, lurd);
            }
    }
    public static void draw_stone(Canvas canvas, Paint p, Rect rect, int y, int x)
    {
        int x1 = x+size, y1 = y+size;

        //красный кубик
        p.setColor(Color.RED);
        p.setStrokeWidth(0);
        p.setStyle(Paint.Style.FILL);
        rect.set(x, y, x1, y1);
        canvas.drawRect(rect, p);//

        p.setStrokeWidth(1);
        rect.set(x+1, y+1, x1-1, y1-1);
        p.setARGB(200,255,255,0);
        p.setStyle(Paint.Style.STROKE);
        canvas.drawRect(rect, p);

        //белая каемка
        rect.set(x, y, x1, y1);
        p.setARGB(100,255,250,250);
        p.setStyle(Paint.Style.STROKE);
        canvas.drawRect(rect, p);//

        if (size > 11)
        {
            //оранжевая каемка
            rect.set(x + 2, y + 2, x1 - 2, y1 - 2);
            p.setARGB(255, 255, 100, 0);
            p.setStyle(Paint.Style.STROKE);
            canvas.drawRect(rect, p);

            p.setStrokeWidth(2);
            rect.set(x + 3, y + 3, x1 - 3, y1 - 3);
            p.setARGB(255, 255, 70, 0);
            p.setStyle(Paint.Style.STROKE);
            canvas.drawRect(rect, p);//
        }


    }
    public static void draw_lawn(Canvas canvas, Paint p, Rect rect, int y, int x, maze m, object ob, Rect rectClip, byte[] lurd)
    {
        int x1 = x+size, y1 = y+size;

        //кубик
        /*switch (mult)
        {
            case 1: p.setARGB(200, 100, 50, 0); break;
            case 2: p.setARGB(200, 255, 105, 180); break;
            case 3: p.setARGB(200, 238, 130, 238); break;
            case 4: p.setARGB(200, 216, 191, 216); break;
            case 5: p.setARGB(200, 245, 245, 220); break;
            case 6: p.setARGB(200, 255, 215, 0); break;
            case 7: p.setARGB(200, 240, 230, 140); break;
            case 8: p.setARGB(200, 173, 255, 47); break;
            case 9: p.setARGB(200, 127, 255, 212); break;
            case 10: p.setARGB(200, 240, 230, 230); break;
            case 11: p.setARGB(200, 252, 228, 181); break;
            case 12: p.setARGB(200, 0, 191, 255); break;
            case 13: p.setARGB(200, 188, 143, 143); break;
            case 14: p.setARGB(200, 216, 191, 216); break;
            case 15: p.setARGB(200, 255, 127, 80); break;
            case 16: p.setARGB(200, 107, 142, 35); break;
        }*/
        p.setARGB(200, m.elem(ob.y, ob.x) * 20, m.elem(ob.y, ob.x) * 10, m.elem(ob.y, ob.x) * 15);
        int shift = (canvas.getWidth() - (size) * m.get().x) / 2;
        rectClip.set((ob.x + lurd[0]) * size + shift - 1, (ob.y + lurd[2]) * size, (ob.x + lurd[3]) * size + size + shift - 1, (ob.y + lurd[3]) * size + size);
        canvas.clipRect(rectClip);
        //p.setARGB(200, 124, 252, 0);
        p.setStrokeWidth(1);
        p.setStyle(Paint.Style.FILL);
        rect.set(x, y, x1, y1);
        canvas.drawRect(rect, p);//

        //сетка
        p.setColor(Color.BLACK);
        p.setStrokeWidth(0.8f);
        p.setStyle(Paint.Style.STROKE);
        rect.set(x, y, x1, y1);
        canvas.drawRect(rect, p);
    }
    public static void draw_matrix(Canvas canvas, int width, int height)
    {
            /*int size = canvas.getWidth() / (width + 1);
            if ( ((size + 1) * width) > (canvas.getWidth() / width * width) )
                size--;

            int shift = (canvas.getWidth() - (size+1) * width) / 2;

            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                {
                    if ( m.matrix[i][j] != 1 && m.matrix[i][j] != 0 )
                        draw_lawn(canvas, i * size + i, j * size + shift + j, size);
                }*/
    }
    public static void draw_player(Canvas canvas, maze m, Paint p, object ob, Rect rectClip)
    {
        RectF rectf = new RectF();
        int shift = (canvas.getWidth() - (size) * m.get().x) / 2;

        p.setColor(Color.YELLOW);
        p.setStrokeWidth(1);
        p.setStyle(Paint.Style.FILL_AND_STROKE);

        rectf.set(ob.x*size+shift-1, ob.y*size, ob.x*size+size+shift-1, ob.y*size+size);
        canvas.drawOval(rectf, p);
        canvas.restore();
    }
    public static void move_player(maze m, object player, float evX_start, float evY_start, float evX_end, float evY_end, byte[] lurd)
    {
        float on_y, on_x;
        on_y = evY_end - evY_start;
        on_x = evX_end - evX_start;
        Rect rectClip = new Rect();
        if ( Math.abs(on_x) > Math.abs(on_y) ) {
            if (on_x > 0) {
                //right
                if (m.elem(player.y, player.x + 1) != 0) {
                    player.x++;
                    lurd[0] = 0;
                    lurd[1] = 0;
                    lurd[2] = 1;
                    lurd[3] = 0;
                }
            } else {
                //left
                if (m.elem(player.y, player.x - 1) != 0) {
                    player.x--;
                    lurd[0] = -1;
                    lurd[1] = 0;
                    lurd[2] = 0;
                    lurd[3] = 0;
                }
            }
        }
        else {
            if (on_y > 0) {
                //down
                if (m.elem(player.y + 1, player.x) != 0) {
                    player.y++;
                    lurd[0] = 0;
                    lurd[1] = 0;
                    lurd[2] = 0;
                    lurd[3] = 1;
                }
            } else {
                //up
                if (m.elem(player.y - 1, player.x) != 0) {
                    player.y--;
                    lurd[0] = 0;
                    lurd[1] = -1;
                    lurd[2] = 0;
                    lurd[3] = 0;
                }
            }
        }
    }
}
